#include "doolittle.h"

Doolittle::Doolittle(const QString &qr,const int &n)
{
    this->n = n;
    std::stringstream ss(qr.toStdString());
    try{
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                ss >> mp[i][j];
            }
        }
    }catch(...){

    }
}
void Doolittle::Run(){
    for(int i=0;i<n;i++){
            mp1[i][i]=1;
            for(int j=i;j<n;j++){
                int tmp = 0;
                for(int k=0;k<i;k++){
                    tmp += mp1[i][k]*mp2[k][j];
           //         cout << "add:" << i << " " << k << " " << j << endl;
                }
                mp2[i][j] = mp[i][j] - tmp;
          //      cout << "U:" << i << " " << j << " ";
           //     cout << mp2[i][j] << endl;
            }

            for(int j=i+1;j<n;j++){
                int tmp = 0;
                for(int k=0;k<i;k++){
                    tmp += mp1[i][k]*mp2[k][j];
            //        cout << "add:" << i << " " << k << " " << j << endl;
                }
                mp1[j][i] = (mp[j][i] - tmp)/mp2[i][i];
           //     cout << "L:" << j << " " << i << " ";
           //     cout << mp1[j][i] << endl;
            }
        }
}
QString Doolittle::getAns(){
    QString rev;
    for(int i=0;i<n;i++){
        for(int j=0;j<=n;j++){
            if(j>i){
                rev += QString::number(mp2[i][j-1]) + "\t";
            }else{
                rev += QString::number(mp1[i][j]) + "\t";
            }
        }
        rev += "\n";
    }
    return rev;
}
